//
//  LocAudioCell.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2025/1/27.
//

import UIKit
typealias ShareDoneBlock = ()->()
@objc class LocAudioCell: UITableViewCell {
    @objc var doneBlock:ShareDoneBlock?
    var nameLabel:UILabel!
    var createTimeLabel:UILabel!
    var sizeAndDurLabel:UILabel!
    var typeLabel:UILabel!
    var selectBtn:UIButton!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        
        let bgview = UIView()
        bgview.backgroundColor = MYColor.color(withHexString: "#f2f2f2", alpha: 0.8)
        self.contentView.addSubview(bgview)
        bgview.layer.cornerRadius = pixw(p:15)
        bgview.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15));
            make.top.equalTo(pixw(p:10));
            make.right.equalTo(pixw(p:-15));
            make.bottom.equalTo(0);
        }
        
        typeLabel = UILabel()
        typeLabel.text = "  --  "
        typeLabel.font = MY_SCALE_BoldFont(12)
        typeLabel.backgroundColor = MYBlue
        typeLabel.layer.cornerRadius = pixw(p: 20)
        typeLabel.layer.masksToBounds = true
        typeLabel.textColor = UIColor.white
        bgview.addSubview(typeLabel)
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15))
            make.height.equalTo(pixw(p: 40))
            make.centerY.equalTo(bgview)
        }
        
        selectBtn = UIButton(type: .custom)
        selectBtn.setImage(UIImage(systemName: "square.and.arrow.up.fill"), for: .normal)
        selectBtn.tintColor = MYBlue
        selectBtn.addTarget(self, action: #selector(selectBtnCilck), for: .touchUpInside)
        bgview.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.right.equalTo(pixw(p:-15));
            make.centerY.equalTo(bgview);
            make.width.height.equalTo(pixw(p:40));
        }
        
        nameLabel = UILabel()
        nameLabel.text = "--"
        nameLabel.font = MY_SCALE_SemiboldFONT(14)
        nameLabel.textColor = MYBlock
        nameLabel.lineBreakMode = .byTruncatingMiddle
        bgview.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:90));
            make.top.equalTo(pixw(p:10));
            make.right.equalTo(selectBtn.snp.left).offset(pixw(p:-15));
        }
        
        
        sizeAndDurLabel = UILabel()
        sizeAndDurLabel.text = "0MB  10:10:10"
        sizeAndDurLabel.font = MY_SCALE_RegularFONT(11)
        sizeAndDurLabel.textColor = MYColor.color(withHexString: "#666666")
        bgview.addSubview(sizeAndDurLabel)
        sizeAndDurLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p:2));
        }
        
        createTimeLabel = UILabel()
        createTimeLabel.text = "--"
        createTimeLabel.font = MY_SCALE_RegularFONT(11)
        createTimeLabel.textColor = MYColor.color(withHexString: "#777777")
        createTimeLabel.lineBreakMode = .byTruncatingMiddle
        bgview.addSubview(createTimeLabel)
        createTimeLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel);
            make.top.equalTo(sizeAndDurLabel.snp.bottom).offset(pixw(p:2));
            make.bottom.equalTo(-pixw(p:10));
        }
        
    }
    
    /// 更新数据
    /// - Parameters:
    ///   - name: 名字带后缀
    ///   - docum: 路径
    func updata(name:String,docum:String) {
        let nameArray = name.components(separatedBy: ".")
        typeLabel.text = "  \(nameArray.last ?? "--")  "
        if nameArray.last!.count < 4 {
            typeLabel.text = "    \(nameArray.last ?? "--")    "
        }
        
        if ["rar","zip","7z"].contains(nameArray.last){
            typeLabel.backgroundColor = MYRed
        }else{
            typeLabel.backgroundColor = MYBlue
        }
        if nameArray.count < 2 {
            typeLabel.text = "  \(LocalizedString("文件夹"))  "
            typeLabel.backgroundColor = MYGreen
            selectBtn.setImage(UIImage(named: "com_right"), for: .normal)
        }
        
        
        
        
        let fileManager = FileManager.default
        let path = (Helper.getPath() ?? "") + "/" + docum + "/" + name
        //        let pathUrl = URL(fileURLWithPath: path)
        do {
            let attributes = try fileManager.attributesOfItem(atPath:path)
            
            // 注意：这里我们使用 modificationDate 而不是创建时间，因为创建时间可能不可用
            if let modificationDate = attributes[.modificationDate] as? Date {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                let str = formatter.string(from: modificationDate)
                createTimeLabel.text = "\(str)"
            }
            
            // 从属性字典中获取文件大小
            if let fileSizeNumber = attributes[.size] as? UInt64 {
                let mSize:Float = Float(fileSizeNumber) / 1024 / 1024
                let kSize:Float = Float(fileSizeNumber) / 1024
                var sizeStr = NSString(format: "%dB",fileSizeNumber)
                if fileSizeNumber > 0 {
                    sizeStr = NSString(format: "%.0fK",kSize)
                }
                if mSize > 1 {
                    sizeStr = NSString(format: "%.2fM",mSize)
                }
                
                if nameArray.count < 2 {
                    sizeAndDurLabel.text = ""
                }else{
                    sizeAndDurLabel.text = "\(sizeStr)"
                    let time = Helper.getFileDuratiomn(path)
                    if time != "00:00:00" {
                        sizeAndDurLabel.text = "\(sizeStr)   \(time ?? "00:00:00")"
                    }
                }
            }
            
        } catch {
            // 处理错误，例如文件不存在或无法读取属性
            print("Error retrieving folder attributes")
        }
        nameLabel.text = name
    }
    
    @objc func selectBtnCilck(){
        if((self.doneBlock) != nil){
            self.doneBlock!()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

